package com.epfly.promo.activity.dao;

import com.epfly.promo.activity.entity.ActivityEntity;
import org.apache.ibatis.annotations.*;
import tk.mybatis.mapper.common.BaseMapper;
import tk.mybatis.mapper.common.example.SelectByExampleMapper;

import java.util.List;

@Mapper
public interface ActivityMapper extends BaseMapper<ActivityEntity>, SelectByExampleMapper<ActivityEntity> {


    @Update(value = "update tbl_activity set is_active = #{isActive} where id = #{activityId} and del_flag = 0 ")
    int modifyActivityIsActive(@Param(value = "activityId") Long activityId, @Param(value = "isActive") Byte isActive);

    @Update(value = "update tbl_activity set status = #{status} where id = #{activityId} and del_flag = 0 ")
    int modifyActivityStatus(@Param(value = "activityId") Long activityId, @Param(value = "status") Byte status);

    /**
     * 活动手工终结时，更新活动状态为已终结，同时更新活动结束时间
     * @param activityId 活动ID
     * @param status 活动状态
     * @param activityEndDate 活动结束时间
     */
    @Update(value = "update tbl_activity set status = #{status}, activity_end_time = #{activityEndDate} where id = #{activityId} and del_flag = 0")
    int terminateActivity(@Param(value = "activityId") Long activityId, @Param(value = "status") Byte status, @Param(value = "activityEndDate") Long activityEndDate);


    List<String> findByInPeriod(ActivityEntity sEn);

    List<ActivityEntity> findByActivityIdList(@Param(value = "activityIdList") List<Long> activityIdList);

    @ResultMap(value = "activityMapperBaseResultMap")
    @Select(value = "SELECT id, merchant_id, activity_name, activity_type, discount_type, activity_start_time, activity_end_time, " +
            " activity_object_type, activity_object_type_cascade, store_style, effective_days, is_suite_holiday, effective_order_methods, " +
            " effective_take_methods, remark, is_active, `status`, next_try_time,  " +
            " execute_times, latest_in_running_time, start_time, end_time, exec_result " +
            " FROM tbl_activity where del_flag = 0 and status = 0 and trace_start_status = 0 and activity_start_time <= #{currentTime} and id > #{activityId} limit 200 ")
    List<ActivityEntity> getActivityStartTodoTask(@Param(value = "currentTime") long currentTime, @Param(value = "id") Long activityId);


    @ResultMap(value = "activityMapperBaseResultMap")
    @Select(value = "SELECT id, merchant_id, activity_name, activity_type, discount_type, activity_start_time, activity_end_time, " +
            " activity_object_type, activity_object_type_cascade, store_style, effective_days, is_suite_holiday, effective_order_methods, " +
            " effective_take_methods, remark, is_active, `status`, next_try_time,  " +
            " execute_times, latest_in_running_time, start_time, end_time, exec_result " +
            " FROM tbl_activity where del_flag = 0 and status = 1 and trace_end_status = 0 and activity_end_time > #{currentTime} and id > #{activityId} limit 200 ")
    List<ActivityEntity> getActivityEndTodoTask(@Param(value = "currentTime") long currentTime, @Param(value = "id") Long activityId);


    int updateTraceStartStatus(@Param(value = "activityIdList") List<Long> activityIdList);


    int updateTraceEndStatus(@Param(value = "activityIdList") List<Long> activityIdList);
}
